package com.guldan.thrall.web.controller;

import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.response.ResponseJson;
import com.guldan.core.search.Pagination;
import com.guldan.jaina.dto.grole.GRoleDTO;
import com.guldan.jaina.dto.grole.GRoleSearchDTO;
import com.guldan.jaina.provider.IAuthorityProvider;
import com.guldan.thrall.common.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/11/17
 * Time: 0:07
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    private IAuthorityProvider iAuthorityProvider;

    //获取所有的角色列表
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    List<GRoleDTO> list(@RequestBody GRoleSearchDTO dto) {
        ResponseDTO<List<GRoleDTO>> response = iAuthorityProvider.listAllRole();
        return response.getData();
    }

    //获取某个角色信息
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    GRoleDTO get(@RequestBody GRoleDTO dto) {
        ResponseDTO<GRoleDTO> response = iAuthorityProvider.getRole(dto.getId());
        return response.getData();
    }

    //新增/保存某个角色信息
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    GRoleDTO save(@RequestBody GRoleDTO dto) throws Exception {
        ResponseDTO<GRoleDTO> response = iAuthorityProvider.saveRole(dto);
        if (response.isSuccess())
            return response.getData();
        else {
            throw response.getException();
        }
    }

    //新增/保存某个角色信息
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    Boolean delete(@RequestBody GRoleDTO dto) {
        ResponseDTO<Boolean> response = iAuthorityProvider.deleteRole(dto.getId());
        return response.getData();
    }
}
